package com.kirk.cts.ui.utils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.net.URL;

/**
 * <p> ButtonUtil.java </p>
 *
 * @version 1.0.0
 * @Author kirkliu
 * @Date 2020-09-01 23:11
 * @Description 按钮工具类
 */
public class ButtonUtil {
    public static JButton makeNavigationButton(String imgLocation,
                                               String actionCommand, String toolTipText, String altText,
                                               ActionListener listener) {
        // Look for the image.

        // Create and initialize the button.
        JButton button = new JButton();
        button.setActionCommand(actionCommand);
        button.setToolTipText(toolTipText);
        button.addActionListener(listener);
        if (altText != null) {
            button.setText(altText);
        }
        if (imgLocation != null) {
            // button.setIconTextGap(10);
            URL imageURL = ButtonUtil.class.getClassLoader().getResource(
                    imgLocation);
            if (imageURL != null) {
                button.setIcon(new ImageIcon(imageURL, altText));
                button.setSize(new Dimension(1, 1));

                // setOpaque(false);// image found
                button.setBorderPainted(false);
                // button.setContentAreaFilled(false);
                // button.setRolloverEnabled(true);
                button.setVerticalTextPosition(JButton.BOTTOM);
                button.setHorizontalTextPosition(JButton.CENTER);
                button.setIconTextGap(0);

            } else { // no image found
                System.err.println("Resource not found: " + imgLocation);
            }

        }

        return button;
    }
}
